<template>
  <div class="page-container">
    <el-form
      class="el-form-modify"
      ref="form"
      label-width="80px"
      :model="formData"
      :rules="rules"
    >
      <el-form-item label="角色" prop="name">
        <el-input v-model="formData.name" :disabled="!!editData" placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item label="选择权限">
        <el-tree
					:data="actionList"
					show-checkbox
					node-key="id"
					:default-checked-keys="formData.actionIds"
					:props="defaultProps"
					@check="onCheck"
				></el-tree>
      </el-form-item>
      <el-form-item v-if="!editData" label="备注" prop="remark">
        <el-input
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 8 }"
          placeholder="请输入角色备注"
          v-model="formData.remark"
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save" size="small" :loading="saving"
          >保存</el-button
        >
        <el-button @click="cancel" size="small">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { menuQuery } from "@/api/menu";
import { roleAdd, getActionsByRole, editRoleActions } from "@/api/roles";
import {rules} from '@/utils/validate'

export default {
  name: "RoleForm",
  props: ['editData'],
  data() {
    return {
      formData: {
        id: 0,
        name:  "",
        actionIds: [],
        remark: "",
        ...(this.editData || {})
      },
      rules: {
        name: [rules.text2_18],
        remark: [rules.textMax_64]
      },
      actionList: [],
      defaultProps: {
				children: "children",
				label: "name"
			},
      saving: false
    };
  },
  methods: {
    onCheck(data, { checkedKeys }) {
			this.formData.actionIds = checkedKeys;
		},
    async requestActions(roleId) {
			try {
				const { data } = await menuQuery();
				this.actionList = data;
			} catch (error) {
				console.log(error);
			}
		},
		async requestActionsByRole(roleId) {
			try {
				const { data } = await getActionsByRole(roleId);
				this.formData.actionIds = data
			} catch (error) {
				console.log(error);
			}
		},
    save() {
      this.$refs.form.validate(async valid => {
        if (!valid) {
          return;
        }
        try {
          this.saving = true;
          if (!this.formData.id) {
            await roleAdd(this.formData)
          } else {
            await editRoleActions(this.formData.id, this.formData.actionIds)
          }
          // const method = this.formData.id ? roleEdit : roleAdd;
          // await method(this.formData);
          this.$message.success('保存成功')
          this.saving = false;
          this.$emit("refresh");
          this.cancel();
        } catch (err) {
          this.saving = false;
        }
      });
    },
    cancel() {
      this.$router.go(-1);
    }
  },
  created() {
    this.requestActions();
    if (this.$route.name == 'editRole' && !this.editData) {
      this.cancel()
      return
    }
    if (this.editData && this.editData.id) {
      this.requestActionsByRole(this.editData.id)
    }
  }
};
</script>

<style lang="scss">
.pagination-title {
  font-weight: normal;
}
.OrderDetails {
  display: flex;
  justify-content: space-between;
}
</style>
